package com.lachesis.molecule.web.interceptor;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/** 
 * @ClassName: CommonInterceptor 
 * @Description: Molecule的基础拦截器，使用依赖于Molecule的项目必须在Spring中申明此拦截器
 * @author shiwei.hu shiwei.hu@lachesis.com
 * @date 2017年10月31日 下午6:53:39 
 *  
 */
public class CommonInterceptor extends HandlerInterceptorAdapter {


    /** 
     * @Fields interceptorList : 拦截器列表 
     */ 
    public List<HandlerInterceptorAdapter> interceptorList;


    /** 
     * <p>Title: 默认构造</p> 
     * <p>Description: 在这里声明需要添加的拦截器实例 </p>  
     */
    public CommonInterceptor()
    {
        this.interceptorList = new ArrayList<HandlerInterceptorAdapter>();
        this.interceptorList.add(new CrossDomainInterceptor());
        this.interceptorList.add(new ApiMonitorInterceptor());
        this.interceptorList.add(new RequestLogInterceptor());
    }


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

        boolean result = true;
        for(HandlerInterceptorAdapter interceptor : this.interceptorList) {

            result = result && interceptor.preHandle(request, response, handler);
        }       

        return result;        
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {

        
        for(HandlerInterceptorAdapter interceptor : this.interceptorList) {
            interceptor.postHandle(request, response, handler, modelAndView);
        } 
    }



}

